package cn.itsource.blog.controller;

import cn.itsource.blog.anno.PreAuthorize;
import cn.itsource.blog.domain.User;
import cn.itsource.blog.query.UserQuery;
import cn.itsource.blog.service.IUserService;
import cn.itsource.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @PatchMapping
    public JsonResult queryPage(@RequestBody UserQuery userQuery) {
        try{
            return JsonResult.success(userService.pageInfo(userQuery));
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }

    @PutMapping
    @PreAuthorize(name = "用户新增",sn = "user:save",descs = "就是测试2把")
    public JsonResult save(@RequestBody User user) {
        try{
            userService.save(user);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }



    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id) {
        try{
            userService.delete(id);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }

    @PatchMapping("/batchDelete")
    public JsonResult batchDelete(@RequestBody List<User> users){
        try{
            userService.batchDelete(users);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }
}
